
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 10/27/2012 15:54:27
-- Generated from EDMX file: c:\users\巍\documents\visual studio 2012\Projects\AzureServerHost\AzureServerHost\Models.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [AzureServerHost];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ProductVirtualMachine]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Product] DROP CONSTRAINT [FK_ProductVirtualMachine];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderAccount]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderDetail] DROP CONSTRAINT [FK_OrderAccount];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderDetailProduct]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderDetail] DROP CONSTRAINT [FK_OrderDetailProduct];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Account]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Account];
GO
IF OBJECT_ID(N'[dbo].[VirtualMachine]', 'U') IS NOT NULL
    DROP TABLE [dbo].[VirtualMachine];
GO
IF OBJECT_ID(N'[dbo].[Product]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Product];
GO
IF OBJECT_ID(N'[dbo].[OrderDetail]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderDetail];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Account'
CREATE TABLE [dbo].[Account] (
    [ID] uniqueidentifier  NOT NULL,
    [LoginName] nvarchar(max)  NOT NULL,
    [Created] datetime  NULL,
    [Updated] datetime  NULL,
    [Email] nvarchar(max)  NULL,
    [UserId] int  NOT NULL
);
GO

-- Creating table 'VirtualMachine'
CREATE TABLE [dbo].[VirtualMachine] (
    [ID] uniqueidentifier  NOT NULL,
    [Updated] datetime  NULL,
    [Created] datetime  NULL,
    [HostName] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Product'
CREATE TABLE [dbo].[Product] (
    [ID] uniqueidentifier  NOT NULL,
    [Updated] datetime  NULL,
    [Created] datetime  NULL,
    [Title] nvarchar(max)  NOT NULL,
    [VirtualMachineID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'OrderDetail'
CREATE TABLE [dbo].[OrderDetail] (
    [ID] uniqueidentifier  NOT NULL,
    [Updated] datetime  NULL,
    [Created] datetime  NULL,
    [AccountID] uniqueidentifier  NOT NULL,
    [AccountLoginName] nvarchar(max)  NOT NULL,
    [ProductID] uniqueidentifier  NOT NULL,
    [Month] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID], [LoginName] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [PK_Account]
    PRIMARY KEY CLUSTERED ([ID], [LoginName] ASC);
GO

-- Creating primary key on [ID] in table 'VirtualMachine'
ALTER TABLE [dbo].[VirtualMachine]
ADD CONSTRAINT [PK_VirtualMachine]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [PK_Product]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'OrderDetail'
ALTER TABLE [dbo].[OrderDetail]
ADD CONSTRAINT [PK_OrderDetail]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [VirtualMachineID] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [FK_ProductVirtualMachine]
    FOREIGN KEY ([VirtualMachineID])
    REFERENCES [dbo].[VirtualMachine]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductVirtualMachine'
CREATE INDEX [IX_FK_ProductVirtualMachine]
ON [dbo].[Product]
    ([VirtualMachineID]);
GO

-- Creating foreign key on [AccountID], [AccountLoginName] in table 'OrderDetail'
ALTER TABLE [dbo].[OrderDetail]
ADD CONSTRAINT [FK_OrderAccount]
    FOREIGN KEY ([AccountID], [AccountLoginName])
    REFERENCES [dbo].[Account]
        ([ID], [LoginName])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderAccount'
CREATE INDEX [IX_FK_OrderAccount]
ON [dbo].[OrderDetail]
    ([AccountID], [AccountLoginName]);
GO

-- Creating foreign key on [ProductID] in table 'OrderDetail'
ALTER TABLE [dbo].[OrderDetail]
ADD CONSTRAINT [FK_OrderDetailProduct]
    FOREIGN KEY ([ProductID])
    REFERENCES [dbo].[Product]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderDetailProduct'
CREATE INDEX [IX_FK_OrderDetailProduct]
ON [dbo].[OrderDetail]
    ([ProductID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------